package zhoufu.sz.sanxiao_check.ui.basic;

import android.app.Dialog;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import java.util.List;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityBasicItem4Binding;
import zhoufu.sz.sanxiao_check.databinding.ActivityShowDialogBinding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.SecureImgEntity;
import zhoufu.sz.sanxiao_check.models.SecureModel;
import zhoufu.sz.sanxiao_check.presents.BasicItem4Presenter;
import zhoufu.sz.sanxiao_check.ui.contract.BasicItem4Contract;
import zhoufu.sz.sanxiao_check.utils.TextUtils;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 * 分级评定表
 */
public class BasicItem4Activity extends BaseMsgActivity implements View.OnClickListener,
        BasicItem4Contract.View<SecureModel>
{
    @Inject
    BasicItem4Presenter _presenter;

    ActivityBasicItem4Binding _binding;

    private FunctionConfig config;

    private Dialog _dialog;

    ActivityShowDialogBinding binding;
    LoginComponet             loginComponet;

    @Override protected void initInject()
    {
        config = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setMutiSelectMaxSize(1)
                .build();
        setTitle(R.string.title_basic_item4);

        loginComponet = DaggerLoginComponet.builder()
                .activityModule(getActivityModule()).appComponent(getAppComponent()).build();
        loginComponet.inject(this);
        _presenter.bidnView(this);
        _presenter.onStart();
    }

    @Override protected void onStart()
    {
        super.onStart();
        _dialogInit();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        super.initAfterOnCreate(binding);
        _binding = (ActivityBasicItem4Binding) binding;
        _binding.setStreetModel(mInfoModel);
        _binding.setOnClickListener(this);
        _binding.setPresenter(_presenter);
    }

    private void _dialogInit()
    {
        if ( _dialog == null )
        {
            _dialog = new Dialog(this, R.style.DialogTheme);
            binding = DataBindingUtil
                    .inflate(LayoutInflater.from(this), R.layout.activity_show_dialog, null, false,
                            LoginComponet.Instance.get());
            _dialog.setContentView(binding.getRoot());
            binding.setOnClickListener(this);
        }
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_basic_item4;
    }


    @Override public void onClick(View v)
    {

        switch (v.getId())
        {
            case R.id.type1Item1:
                showDialog(_presenter.setSecureImgEntity(1, 0));
                break;
            case R.id.type1Item2:
                showDialog(_presenter.setSecureImgEntity(1, 1));
                break;
            case R.id.type1Item3:
                showDialog(_presenter.setSecureImgEntity(1, 2));
                break;
            case R.id.type2Item1:
                showDialog(_presenter.setSecureImgEntity(2, 0));
                break;
            case R.id.type2Item2:
                showDialog(_presenter.setSecureImgEntity(2, 1));
                break;
            case R.id.type2Item3:
                showDialog(_presenter.setSecureImgEntity(2, 2));
                break;
            case R.id.type4Item1:
                showDialog(_presenter.setSecureImgEntity(4, 0));
                break;
            case R.id.type4Item2:
                showDialog(_presenter.setSecureImgEntity(4, 1));
                break;
            case R.id.type4Item3:
                showDialog(_presenter.setSecureImgEntity(4, 2));
                break;
            case R.id.type3Item1:
                showDialog(_presenter.setSecureImgEntity(3, 0));
                break;
            case R.id.type3Item2:
                showDialog(_presenter.setSecureImgEntity(3, 1));
                break;
            case R.id.type3Item3:
                showDialog(_presenter.setSecureImgEntity(3, 2));
                break;
            case R.id.type5Item1:
                showDialog(_presenter.setSecureImgEntity(5, 0));
                break;
            case R.id.type5Item2:
                showDialog(_presenter.setSecureImgEntity(5, 1));
                break;
            case R.id.type5Item3:
                showDialog(_presenter.setSecureImgEntity(5, 2));
                break;
            case R.id.type6Item1:
                showDialog(_presenter.setSecureImgEntity(6, 0));
                break;
            case R.id.type6Item2:
                showDialog(_presenter.setSecureImgEntity(6, 1));
                break;
            case R.id.type6Item3:
                showDialog(_presenter.setSecureImgEntity(6, 2));
                break;
            case R.id.mBtnCompleted:
                _presenter.update(_presenter.getEntity());
                _presenter.getEntity().PhotoPath.set(_presenter.getEntity().PhotoLocal.get());
                _dialog.dismiss();
                break;
            case R.id.mIvShow:
                GalleryFinal.openGalleryMuti(1, config, mResultCallback);
                break;
        }
    }

    private void showDialog(SecureImgEntity entity)
    {
        if ( entity == null )
        {
            UIUtils.showSnackbar(_binding.getRoot(), R.string.toast_network_error);
            return;
        }
        binding.setModel(entity);
        UIUtils.setDialogWindowAttr(_dialog, this);
        if ( TextUtils.isEmpty(entity.PhotoPath.get()) )
        {
            GalleryFinal.openGalleryMuti(1, config, mResultCallback);
            _binding.mRbGrade1.postDelayed(new Runnable()
            {
                @Override public void run()
                {
                    _dialog.show();
                }
            }, 500);
        } else
        {
            _dialog.show();
        }
    }

    @Override public String SID()
    {
        return mInfoModel.getSID();
    }

    @Override public void setModel(SecureModel fireNoticeModel)
    {
        _binding.setModel(fireNoticeModel);
    }

    @Override public void updateSuccese()
    {
        UIUtils.showToast(this, "操作成功");
    }

    @Override public void showDelDialog(final SecureImgEntity entity)
    {
        new AlertDialog.Builder(this).setMessage("确定删除该条隐患吗?").setNegativeButton(R.string.dialog_ok,
                new DialogInterface.OnClickListener()
                {
                    @Override public void onClick(DialogInterface dialog, int which)
                    {
                        entity.Describe.set("");
                        entity.PhotoLocal.set("0");//后台规定
                        entity.PhotoPath.set("");
                        _presenter.update(entity);
                        dialog.dismiss();
                    }
                }).create().show();
    }

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        UIUtils.showSnackbar(_binding.getRoot(), R.string.toast_network_error);
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }

    @Override protected void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }

    private GalleryFinal.OnHanlderResultCallback mResultCallback
            = new GalleryFinal.OnHanlderResultCallback()
    {
        @Override public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList)
        {
            if ( resultList != null && resultList.size() > 0 )
            {
                PhotoInfo info = resultList.get(0);
                _presenter.getEntity().PhotoLocal.set(info.getPhotoPath());
            } else
            {
                _dialog.dismiss();
            }
        }

        @Override public void onHanlderFailure(int requestCode, String errorMsg)
        {
            UIUtils.showSnackbar(_binding.getRoot(), errorMsg);
            _dialog.dismiss();
        }
    };
}
